﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Computer_Manager.BUS;
using Computer_Manager.DTO;

namespace Computer_Manager.GUI
{
    public partial class v : DevExpress.XtraEditors.XtraForm
    {
        DTO_categories dto_cate = new DTO_categories();
        BUS_categories bus_cate = new BUS_categories();
        frmCategories obj = (frmCategories)Application.OpenForms["frmCategories"];
        public v()
        {
            InitializeComponent();
        }

        private void v_Load(object sender, EventArgs e)
        {
            dto_cate.parent_id = 0;
            cmbCat.DataSource = bus_cate.GetByParentId(dto_cate);
            cmbCat.DisplayMember = "name_cat";
            cmbCat.ValueMember = "id_cat";
            cmbCat.SelectedIndex = -1;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtCode.Text = "";
            txtName.Text = "";
            cmbCat.SelectedIndex = -1;
        }

        private void v_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtCode.Text == "" || txtName.Text == "" || cmbCat.SelectedIndex == -1)
            {
                if (txtCode.Text == "")
                {
                    txtCode.Properties.NullValuePrompt = "Vui lòng nhập mã loại hàng";
                    txtCode.BackColor = Color.NavajoWhite;
                }
                else
                {
                    txtCode.Properties.NullValuePrompt = "";
                    txtCode.BackColor = Color.White;
                }
                /////////
                if (txtName.Text == "")
                {
                    txtName.Properties.NullValuePrompt = "Vui lòng nhập tên loại hàng";
                    txtName.BackColor = Color.NavajoWhite;
                }
                else
                {
                    txtName.Properties.NullValuePrompt = "";
                    txtName.BackColor = Color.White;
                }
                if (cmbCat.SelectedIndex == -1)
                {
                    cmbCat.Text = "Vui lòng chọn danh mục";
                    cmbCat.BackColor = Color.NavajoWhite;
                }
                else
                {
                    cmbCat.Text = "";
                    cmbCat.BackColor = Color.White;
                }
            }
            else
            {
                    dto_cate.codes_cat = txtCode.Text;
                    dto_cate.name_cat = txtName.Text;
                    dto_cate.parent_id = int.Parse(cmbCat.SelectedValue.ToString());
                    bus_cate.categories_insert(dto_cate);
                    MessageBox.Show("Thêm thành công!!!");
                    this.Dispose();
                    this.Close();
                    obj.GridviewCat();
            }
        }
    }
}